<?php
/**
 * This file contains the class for validate ipv4 addresses
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @version   SVN: $Id: validate.ipv4.class.php 110 2010-07-23 14:57:13Z sruttloff@googlemail.com $
 * @link      phpmy
 */

/**
 * class that validates ipv4 addresses
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @link      phpmy
 */
class validateIpv4
{
    /**
     * check a ipv4 for validity
     *
     * @param string $string string to validate
     *
     * @return bool
     */
    public static function validate($string = null)
    {
        $string = trim($string);
        if (!$string)
            return false;
        // explode in segments
        $segs = explode('.', $string);
        if (count($segs) != 4)
            return false;
        // check if each segm. is within 0 and 255
        foreach ($segs as $temp)
        {
            if ((int)$temp < 0 || (int)$temp > 255)
                return false;
        }
        // all fine
        return true;
    }
}